﻿package com.tudou.home.newskin.containers
{
    import com.tudou.home.common.events.*;
    import com.tudou.home.newskin.buttons.*;
    import com.tudou.home.newskin.events.*;
    import com.tudou.skin.*;
    import com.tudou.skin.common.events.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class Progress extends Sprite implements IProgress, IPreviewer, ITime, IVideoAudio, IMove, ITransform, ISkipHeadTail
    {
        private var waitTimerId:uint;
        private var _isAudio:Boolean = false;
        private var timeTip:TimeTip;
        private var arrowMiddleX:int;
        private var progressBar:ProgressBar;
        private var goHeight:uint = 10;
        private var totalTime:Number = 0;
        private var goWidth:uint = 100;
        private var _previewStartDt:int;
        private var previewPercent:Number = 0;
        private var endSeekTime:Number = 0;
        private var previewWidth:uint;
        private var startSeekTime:Number = 0;
        private var status:String = "out";
        private var previewer:PreviewBackgroundMC;
        private var previewHeigth:uint;
        private static const SHOW:String = "show";
        private static const ON_WAIT:String = "onWait";
        private static const WAIT_TIME:Number = 800;
        private static const OUT:String = "out";

        public function Progress()
        {
            var _loc_1:int = 1;
            progressBar = new ProgressBar();
            addChild(progressBar);
            previewer = new PreviewBackgroundMC();
            previewWidth = previewer.width;
            previewHeigth = previewer.height;
            arrowMiddleX = previewer.arrow.x;
            previewer.mouseChildren = false;
            previewer.buttonMode = true;
            previewer.useHandCursor = true;
            previewer.y = _loc_1 - previewHeigth + 4;
            addChild(previewer);
            timeTip = new TimeTip();
            timeTip.y = _loc_1;
            addChild(timeTip);
            progressBar.addEventListener(BarEvent.START_DRAG, barHlr);
            progressBar.addEventListener(BarEvent.DRAGING, barHlr);
            progressBar.addEventListener(BarEvent.END_DRAG, barHlr);
            progressBar.addEventListener(MouseEvent.ROLL_OUT, rollOutHlr);
            progressBar.addEventListener(MouseEvent.ROLL_OVER, rollOverHlr);
            progressBar.addEventListener(SkipEvent.HEAD, skipMarkHlr);
            progressBar.addEventListener(SkipEvent.TAIL, skipMarkHlr);
            previewer.addEventListener(MouseEvent.ROLL_OUT, previewRollOutHlr);
            previewer.addEventListener(MouseEvent.CLICK, previewClickHlr);
            setTimeTip(0);
            timeTip.visible = false;
            timeTip.loading = false;
            previewer.visible = false;
            progressBar.setPlayed(0);
            progressBar.setLoaded(0);
            return;
        }// end function

        public function setSkipHeadTailValue(param1:Number, param2:Number) : void
        {
            progressBar.setSkipHeadTailValue(param1, param2);
            return;
        }// end function

        private function previewRollOutHlr(event:MouseEvent) : void
        {
            previewer.visible = false;
            status = OUT;
            sendPreviewDt();
            dispatchEvent(new SkinEvent(SkinEvent.CLOSE_PREVIEW));
            return;
        }// end function

        public function setLoaded(param1:Number) : void
        {
            param1 = MathUtil.unit(param1);
            progressBar.setLoaded(param1);
            return;
        }// end function

        public function isDragging() : Boolean
        {
            return progressBar.isDragging();
        }// end function

        private function rollOutHlr(event:MouseEvent) : void
        {
            timeTip.visible = false;
            timeTip.loading = false;
            progressBar.removeEventListener(MouseEvent.MOUSE_MOVE, moveHlr);
            setTimeTip(event.stageX);
            if (status == ON_WAIT)
            {
                clearTimeout(waitTimerId);
                waitTimerId = 0;
                status = OUT;
            }
            else if (status == SHOW)
            {
                if (event.relatedObject && event.relatedObject == previewer)
                {
                }
                else
                {
                    previewer.visible = false;
                    status = OUT;
                    sendPreviewDt();
                    dispatchEvent(new SkinEvent(SkinEvent.CLOSE_PREVIEW));
                }
            }
            return;
        }// end function

        public function setPlayTime(param1:Number) : void
        {
            return;
        }// end function

        public function hasSkipHeadTail(param1:Boolean) : void
        {
            progressBar.hasSkipHeadTail(param1);
            return;
        }// end function

        public function showPreview() : void
        {
            timeTip.visible = false;
            timeTip.loading = false;
            previewer.visible = true;
            return;
        }// end function

        private function previewClickHlr(event:MouseEvent) : void
        {
            dispatchEvent(new SkinEvent(SkinEvent.END_SEEK, previewPercent));
            previewer.visible = false;
            status = OUT;
            sendPreviewDt();
            dispatchEvent(new SkinEvent(SkinEvent.CLOSE_PREVIEW));
            return;
        }// end function

        public function getPlayHead() : Number
        {
            return progressBar.getValue();
        }// end function

        private function rollOverHlr(event:MouseEvent) : void
        {
            timeTip.visible = true;
            timeTip.loading = false;
            progressBar.addEventListener(MouseEvent.MOUSE_MOVE, moveHlr);
            setTimeTip(event.stageX);
            if (!_isAudio)
            {
                waitTimerId = setTimeout(timeOutHlr, WAIT_TIME);
                status = ON_WAIT;
            }
            return;
        }// end function

        public function setStarted(param1:Number) : void
        {
            param1 = MathUtil.unit(param1);
            progressBar.setStarted(param1);
            return;
        }// end function

        public function setPreviewer(param1:DisplayObject) : void
        {
            param1.width = 125;
            param1.height = 93;
            param1.x = 4;
            param1.y = 4;
            this.previewer.addChild(param1);
            return;
        }// end function

        private function initPreviewDt() : void
        {
            _previewStartDt = getTimer();
            return;
        }// end function

        public function canMoveOut() : Boolean
        {
            return status != SHOW;
        }// end function

        private function sendPreviewDt() : void
        {
            var _loc_1:* = getTimer() - _previewStartDt;
            if (_loc_1 < 0)
            {
                _loc_1 = 0;
            }
            if (_loc_1 > 0)
            {
                dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.PREVIEW_DT + "|" + _loc_1));
            }
            return;
        }// end function

        public function move(param1:Number) : void
        {
            return;
        }// end function

        private function setTimeTip(param1:Number) : void
        {
            var _loc_6:int = 0;
            var _loc_2:* = progressBar.globalToLocal(new Point(param1));
            previewPercent = progressBar.getValueByX(_loc_2.x);
            timeTip.timeText.text = TimeUtil.second2Minute(previewPercent * totalTime);
            timeTip.x = _loc_2.x;
            var _loc_3:* = int(timeTip.x - previewWidth / 2);
            var _loc_4:int = -18;
            var _loc_5:* = goWidth - previewWidth + 18;
            if (_loc_3 < _loc_4)
            {
                previewer.x = _loc_4;
                _loc_6 = arrowMiddleX + _loc_3 - _loc_4;
                previewer.arrow.x = _loc_6;
            }
            else if (_loc_3 > _loc_5)
            {
                previewer.x = _loc_5;
                _loc_6 = arrowMiddleX + _loc_3 - _loc_5;
                previewer.arrow.x = _loc_6;
            }
            else
            {
                previewer.x = _loc_3;
                previewer.arrow.x = arrowMiddleX;
            }
            return;
        }// end function

        public function setPlayed(param1:Number) : void
        {
            param1 = MathUtil.unit(param1);
            if (!progressBar.isDragging())
            {
                progressBar.setPlayed(param1);
            }
            return;
        }// end function

        public function addVideo(param1:DisplayObject) : void
        {
            return;
        }// end function

        private function barHlr(event:BarEvent) : void
        {
            var _loc_2:String = null;
            progressBar.setValue(event.value);
            switch(event.type)
            {
                case BarEvent.START_DRAG:
                {
                    startSeekTime = getTimer();
                    _loc_2 = SkinEvent.START_SEEK;
                    break;
                }
                case BarEvent.DRAGING:
                {
                    _loc_2 = SkinEvent.SEEKING;
                    break;
                }
                case BarEvent.END_DRAG:
                {
                    _loc_2 = SkinEvent.END_SEEK;
                    endSeekTime = getTimer();
                    break;
                }
                default:
                {
                    trace("attention here!");
                    break;
                }
            }
            dispatchEvent(new SkinEvent(_loc_2, event.value));
            if (SkinEvent.END_SEEK == _loc_2)
            {
                dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.PROGRESS + "|" + (endSeekTime - startSeekTime) + "|" + event.value));
            }
            return;
        }// end function

        private function skipMarkHlr(event:SkipEvent) : void
        {
            dispatchEvent(new SkinEvent(SkinEvent.OVER_SKIP_MARKER));
            return;
        }// end function

        public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            goWidth = param1;
            goHeight = param2;
            progressBar.goTransform(param1, param2, param3);
            return;
        }// end function

        private function moveHlr(event:MouseEvent) : void
        {
            setTimeTip(event.stageX);
            if (status == ON_WAIT)
            {
                clearTimeout(waitTimerId);
                waitTimerId = setTimeout(timeOutHlr, WAIT_TIME);
                status = ON_WAIT;
            }
            else if (status == SHOW)
            {
                timeTip.visible = true;
                timeTip.loading = false;
                previewer.visible = false;
                waitTimerId = setTimeout(timeOutHlr, WAIT_TIME);
                status = ON_WAIT;
                sendPreviewDt();
                dispatchEvent(new SkinEvent(SkinEvent.CLOSE_PREVIEW));
            }
            return;
        }// end function

        private function timeOutHlr() : void
        {
            if (status == ON_WAIT)
            {
                timeTip.loading = true;
                clearTimeout(waitTimerId);
                waitTimerId = 0;
                status = SHOW;
                previewer.timeText.text = timeTip.timeText.text;
                initPreviewDt();
                dispatchEvent(new SkinEvent(SkinEvent.PREVIEW, previewPercent));
            }
            return;
        }// end function

        public function setTotalTime(param1:Number) : void
        {
            totalTime = param1;
            return;
        }// end function

        public function isAudio(param1:Boolean) : void
        {
            _isAudio = param1;
            return;
        }// end function

    }
}
